<template>
  <van-sticky>
    <div class="kickback_header">
      <div class="left">
        <div>
          可提现(元): <span>{{ CashableAmount }}</span>
        </div>
        <div>
          累计提现(元): <span>{{ CumulativeAmount }}</span>
        </div>
      </div>
      <div class="right" @click="OnCashable('all')">立即提现</div>
    </div>

    <van-tabs
      v-model:active="TabActive"
      type="card"
      sticky
      background="#288BD6"
      @click-tab="OnTab"
    >
      <van-tab
        v-for="item in TabList"
        :title="item.title"
        :name="item.status"
        :key="item.status"
      ></van-tab>
    </van-tabs>
  </van-sticky>

  <div class="kickback">
    <van-pull-refresh v-model="refreshing" @refresh="OnRefresh">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="OnLoad"
      >
        <div class="list">
          <div
            class="item"
            v-for="(item, index) in list"
            :key="item.id"
            @click="item.status == 0 ? OnCashable(item.id, index) : null"
          >
            <div class="left">
              <div class="img">
                <img :src="item.product?.thumb_text" />
              </div>
              <div class="content">
                <div class="nickname">
                  订单号：{{ item.product_order?.code }}
                </div>
                <div class="tel">商品名称：{{ item.product?.name }}</div>
              </div>
            </div>
            <div class="right">
              <div class="status">{{ item.status_text }}</div>
              <div class="money">{{ item.amount }}</div>
            </div>
          </div>

          <van-back-top :bottom="'80'" />
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script setup>
// this代理
const { proxy } = getCurrentInstance();

// 用户信息
const business = proxy.$business;
// 分销会员数据
const list = ref([]);
// 刷新状态
const refreshing = ref(false);
// 懒加载状态
const loading = ref(false);
// 加载状态
const finished = ref(false);
// 当前页
const page = ref(1);
// tab选中状态
const TabActive = ref(0);
// tab数据
const TabList = ref([
  {
    title: "未提现",
    status: 0,
  },
  {
    title: "已提现",
    status: 1,
  },
]);
// 可提现
const CashableAmount = ref("0.00");
// 累计提现
const CumulativeAmount = ref("0.00");

// 挂载前钩子
onBeforeMount(() => {
  // 先执行一次下拉刷新函数，获取数据
  OnRefresh();
});

// 切换Tab栏
const OnTab = debounce(() => {
  OnRefresh();
}, 1000);

// 下拉刷新回调函数
const OnRefresh = () => {
  page.value = 1;
  list.value = [];
  finished.value = false;
  loading.value = true;
  refreshing.value = true;

  setTimeout(OnLoad, 1000);
};

// 上拉加载回调函数
const OnLoad = async () => {
  // 如果是下拉刷新，重置其值
  if (refreshing.value === true) {
    refreshing.value = false;
  }
  // 组装参数
  let params = {
    busid: business.value.id,
    status: TabActive.value,
    page: page.value,
  };
  // 发起请求
  let result = await proxy.$http.post({
    url: "/sale/money",
    params,
  });
  // 取消加载
  loading.value = false;
  // 处理请求结果
  if (result.code === 1) {
    // 获取数据总数
    let count = result.data ? result.data.count : 0;
    // 获取数据并且合并数组
    list.value = result.data ? list.value.concat(result.data.list) : [];
    // 获取累计提现金额
    CumulativeAmount.value = result.data
      ? result.data.amount.toFixed(2)
      : "0.00";
    // 获取可提现金额
    CashableAmount.value = result.data
      ? result.data.cashable.toFixed(2)
      : "0.00";
    // 判断是否加载完所有数据
    if (list.value.length >= count) {
      // 完成加载
      finished.value = true;
      return false;
    }
    // 分页+1
    page.value = page.value + 1;
  } else {
    // 获取累计提现金额
    CumulativeAmount.value = result.data
      ? result.data.amount.toFixed(2)
      : "0.00";
    // 获取可提现金额
    CashableAmount.value = result.data
      ? result.data.cashable.toFixed(2)
      : "0.00";
    finished.value = true;
  }
};

// 提现
const OnCashable = (val, index) => {
  // console.log(val);
  let obj = {};
  if (val == "all") {
    obj = {
      title: "提现",
      message: "是否确认提现全部佣金？",
    };
  } else {
    obj = {
      title: "提现",
      message: "是否确认提现该佣金？",
    };
  }
  // 确认框
  proxy
    .$confirm(obj)
    .then(async () => {
      // 组装参数
      let params = {
        busid: business.value.id,
      };
      if (val != "all") {
        params.id = val;
      }
      // 发起请求
      let result = await proxy.$http.post({
        url: "/sale/cashable",
        params,
      });
      if (result.code === 1) {
        proxy.$success(result.msg);
        if (val === "all") {
          // 提现全部 直接刷新页面
          OnRefresh();
        } else {
          // 提现单个 无刷新更新响应式数据
          list.value.splice(index, 1);
        }
      } else {
        proxy.$fail(result.msg);
      }
    })
    .catch(() => {});
};
</script>

<style scoped>
@import url("/assets/css/SaleMoney.css");
</style>